package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Tag(name = "房间属性管理")
@RestController
@RequestMapping("/admin/attr")
@Slf4j
public class AttrController {

    @Autowired
    private AttrKeyService attrKeyService;

    @Autowired
    private AttrValueService attrValueService;

    @Operation(summary = "新增或更新属性名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {
        attrKeyService.saveOrUpdate(attrKey);
        return Result.ok();
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {
        attrValueService.saveOrUpdate(attrValue);
        return Result.ok();
    }


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> listAttrInfo() {
        // 使用stream流，将所有数据取出再进行数据组装 costTime：418ms 缓存后 20ms内
        /* long startTime = System.currentTimeMillis();

        List<AttrKey> keyList = attrKeyService.list();
        Map<Long, List<AttrValue>> valueListMap = attrValueService.list().stream()
                .collect(Collectors.groupingBy(AttrValue::getAttrKeyId));

        List<AttrKeyVo> attrKeyVoArrayList = keyList.stream().map(key -> {
            AttrKeyVo attrKeyVo = new AttrKeyVo();
            BeanUtils.copyProperties(key, attrKeyVo);
            attrKeyVo.setAttrValueList(valueListMap.get(attrKeyVo.getId()));
            return attrKeyVo;
        }).collect(Collectors.toList());

        long endTime = System.currentTimeMillis();
        log.info("查询全部属性名称和属性值列表 costTime:"+(endTime - startTime) + "ms");*/

        // 在面对大量数据时，通常 SQL 联表查询的效率更高。
        // 这是因为 SQL 联表查询在数据库层面进行了优化，可以一次性获取所需的数据，减少了网络传输和数据处理的开销。

        // 使用sql进行查询 直接使用左连接查询  costTime:345ms; 缓存后 10ms内
        long startTime = System.currentTimeMillis();
        ArrayList<AttrKeyVo> attrKeyVoArrayList = attrKeyService.listAttrKeyVo();
        long endTime = System.currentTimeMillis();
        log.info("查询全部属性名称和属性值列表 costTime:"+(endTime - startTime) + "ms");

        return Result.ok(attrKeyVoArrayList);
    }

    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping("key/deleteById")
    @Transactional
    public Result removeAttrKeyById(@RequestParam Long attrKeyId) {
        attrKeyService.removeById(attrKeyId);
        attrValueService.remove(new LambdaQueryWrapper<AttrValue>().eq(AttrValue::getAttrKeyId, attrKeyId));
        return Result.ok();
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("value/deleteById")
    public Result removeAttrValueById(@RequestParam Long id) {
        attrValueService.removeById(id);
        return Result.ok();
    }

}
